package com.lxk.mall.pms.controller.admin.comment;

import com.lxk.mall.pms.domain.bo.comment.PmsProductCommentBo;
import com.lxk.mall.pms.domain.bo.comment.ProductCommentReplyReqBO;
import com.lxk.mall.pms.domain.bo.comment.ProductCommentUpdateVisibleReqBO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.log.enums.BusinessType;
import com.lxk.mall.pms.domain.vo.comment.PmsProductCommentVo;
import com.lxk.mall.pms.service.IPmsProductCommentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品评论
 *
 * @author Lion Li
 * @date 2024-06-06
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/pms/comment")
public class PmsProductCommentController extends BaseController {

    private final IPmsProductCommentService pmsProductCommentService;

    /**
     * 查询商品评论列表
     */
    @SaCheckPermission("pms:comment:list")
    @GetMapping("/list")
    public TableDataInfo<PmsProductCommentVo> list(PmsProductCommentBo bo, PageQuery pageQuery) {
        return pmsProductCommentService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取商品评论详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("pms:comment:query")
    @GetMapping("/{id}")
    public R<PmsProductCommentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(pmsProductCommentService.queryById(id));
    }

    /**
     * 新增虚拟评论
     */
    @SaCheckPermission("pms:comment:add")
    @Log(title = "商品评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmsProductCommentBo bo) {
        return toAjax(pmsProductCommentService.insertByBo(bo));
    }

    /**
     * 显示 / 隐藏评论
     * @param updateReqVO
     * @return
     */
    @PutMapping("/update-visible")
    @SaCheckPermission("pms:comment:update")
    public R<Boolean> updateCommentVisible(@Valid @RequestBody ProductCommentUpdateVisibleReqBO updateReqVO) {
        pmsProductCommentService.updateCommentVisible(updateReqVO);
        return R.ok(Boolean.TRUE);
    }

    /**
     * 商家回复
     * @param replyVO
     * @return
     */
    @PutMapping("/reply")
    @SaCheckPermission("pms:comment:update")
    public R<Boolean> commentReply(@Valid @RequestBody ProductCommentReplyReqBO replyVO) {
        pmsProductCommentService.replyComment(replyVO, LoginHelper.getUserId());
        return R.ok(Boolean.TRUE);
    }
}
